# -*- coding: utf-8 -*-
import random
import win32gui as ControlUI1
import win32api as IconUI1
import win32con as Clipboard1
import uiautomation as Gia
import time
from Pack import Logic
from Pack.Logic import NextChildNamelist
from QQRobot import SetQQRobotName,SetQQgroupName

QGName= Logic.Name(SetQQgroupName, SetQQRobotName)
PickList=[]


TextSentenceList=[]
OpenTextSentence=open('Pack/SentenceText.txt','r',encoding="utf-8")
for SentenceElement in OpenTextSentence:
    HandleTextSentence= SentenceElement.strip('\n').replace(' ','').replace('、','/').replace('?','').split('/')
    for ProcessingCompletedSentence in HandleTextSentence:
        TextSentenceList.append(ProcessingCompletedSentence)
OpenTextSentence.close()
print(TextSentenceList)

while True:
    WhilePickList = []
    ChatWindow = Gia.WindowControl(searchDepth=1, ClassName='TXGuiFoundation', Name='消息管理器')
    print(ChatWindow)

    Logic.PrintChildInformation(ChatWindow, 1)
    WhilePickList.extend(NextChildNamelist)
    print(WhilePickList)
    for i in range(1,2) :
        if WhilePickList[-i] in PickList :
            break
        SelectIndexOutput = WhilePickList[-i][WhilePickList[-i].index(':'):]
        print("返回：",SelectIndexOutput)

        if ('介绍' in SelectIndexOutput) and (QGName[1] not in WhilePickList[-i]) :
            Logic.SendMessage(QGName[0], '我在，我是籽怼用Python写的机器人\n 联系籽怼：2702114947')
        elif ('时间' in SelectIndexOutput) and (QGName[1] not in WhilePickList[-i]) :
            time.time()
            timemessage=time.strftime("%Y年%m月%d日 %H:%M:%S", time.localtime())
            timemessage1='你好，现在北京时间：'+timemessage
            Logic.SendMessage(QGName[0], timemessage1)
        elif (('籽怼你说句话啊' in SelectIndexOutput) or ('一语' in SelectIndexOutput) or ('鹿本你说句话啊' in SelectIndexOutput)) and (QGName[1] not in WhilePickList[-i]):
            RandomTextSentence=random.choice(TextSentenceList)
            StrTextSentence=str(RandomTextSentence)
            SectionStrTextSentence=StrTextSentence.strip("[").strip("]").strip("'")
            Logic.SendMessage(QGName[0],SectionStrTextSentence)
        elif('籽怼机器人停止运行' in SelectIndexOutput) and (QGName[1] not in WhilePickList[-i]) :
            Logic.SendMessage(QGName[0], '停...停不下来辣')
            exit()
        elif(QGName[1] not in WhilePickList[-i]) :
            pass

    PickList=WhilePickList

    HwndTitle=dict()
    def GetAll(Gah, Gam):
        if ControlUI1.IsWindow(Gah) and ControlUI1.IsWindowEnabled(Gah) and ControlUI1.IsWindowVisible(Gah):
            HwndTitle.update({Gah: ControlUI1.GetWindowText(Gah)})


    ControlUI1.EnumWindows(GetAll, 0)
    for MessageManager in HwndTitle.items():
        print(MessageManager)
        if MessageManager[1] == '消息管理器':
            break
    long_position = IconUI1.MAKELONG(x, y)
    IconUI1.SendMessage(int(MessageManager[0]), Clipboard1.WM_LBUTTONDOWN, Clipboard1.MK_LBUTTON, long_position)
    IconUI1.SendMessage(int(MessageManager[0]), Clipboard1.WM_LBUTTONUP, Clipboard1.MK_LBUTTON, long_position)
    print(PickList)